//
//  BNHomeDetailController.m
//  BNCodeGuide
//
//  Created by kevin007 on 2020/10/9.
//  Copyright © 2020 kevin007. All rights reserved.
//

#import "BNHomeDetailController.h"

#import "BNHomeDetailListView.h" //list view

#import "BNHomeDetailViewModel.h"  //view model

@interface BNHomeDetailController ()<BNBaseTableListViewDelegate>
/** table list view*/
@property (nonatomic, strong) BNHomeDetailListView *listView;
/** view model*/
@property (nonatomic, strong) BNHomeDetailViewModel *viewModel;
@end

@implementation BNHomeDetailController
- (void)dealloc
{
    NSLog(@"%s", __func__);
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self setupUI];
    [self requestData];
}

#pragma mark - setupUI
- (void)setupUI {
    self.title = @"home-detail";
    self.view.backgroundColor = [UIColor randomColor];
    
    [self.view addSubview:self.listView];
    
    [self addLayouts];
}

#pragma mark - ----------------AddLayouts----------------
- (void)addLayouts {
    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(0);
    }];
}

#pragma mark - ----------------Request----------------
- (void)requestData {
    __weak typeof(self) weakSelf = self;
    [self.viewModel requestDataWithCompletion:^(BOOL succeed, NSString *errorMsg) {
        __strong typeof(self) strongSelf = weakSelf;
        [strongSelf refreshUI];
    }];
}

#pragma mark - ----------------RefreshUI----------------
- (void)refreshUI {
    self.listView.dataSource = self.viewModel.listDatas;
    [self.listView refreshUI];
}

#pragma mark - ----------------BNBaseTableListViewDelegate----------------
- (void)tableListView:(BNBaseTableListView *)listView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __func__);
}

#pragma mark - ----------------Getter & Setter----------------
- (BNHomeDetailListView *)listView {
    if (!_listView) {
        _listView = [[BNHomeDetailListView alloc] initWithFrame:CGRectZero tableViewStyle:UITableViewStyleGrouped];
        _listView.tableView.backgroundColor = [UIColor lightGrayColor];
        _listView.delegate = self;
    }
    
    return _listView;
}

- (BNHomeDetailViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [BNHomeDetailViewModel new];
    }
    
    return _viewModel;
}
@end
